*Analyses_Main
local date 20140509
version 13

cd "/Users/au128248/Library/CloudStorage/OneDrive-AarhusUniversitet/replication"

*log
capture log close _all
log using "./results/partlog", name(partlog) replace nomsg text
log off partlog

use "./gendata/maindata.dta",clear

*******************
**Sample selection*
**Sample loss******
*******************


*sample loss 
su id
sca totN = r(N)
reg AFS
log on partlog
*loss due to DV
di "Loss due to missing on DV (%): " 1-(e(N)/totN)
qui: sca totN = e(N)
*loss due to other
drop if e(sample)!=1
qui: reg AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy ifriends
di "Loss due to missing on other vars (%): " 1-(e(N)/totN)
log off partlog
drop if e(sample)!=1

*****************************************
**Table 1/Aggregated part of Appendix A**
*****************************************
preserve
tab icolleagues1,gen(CD)
tab ifriend,gen(FD)
collapse AFS CD* FD*  Mapmulti MapmultiR Cunemploy Cforeign Cgdpcap ,by(cname)
format AFS-Cforeign %12.2f
format Cgdpcap %12.0f
sort Mapmulti
log on partlog
**Table 1**
list cname AFS CD* FD*  Mapmulti MapmultiR
sum AFS CD* FD*  Mapmulti MapmultiR,format //Total
**Aggregated part of Appendix A
sum Mapmulti MapmultiR Cgdpcap Cforeign Cunemploy ,format
*IQR of party focus
sum Mapmulti,d
log off partlog 
restore


format AFS IM* age lrself2 %12.2f
log on partlog 
*Individual-level part of Appendix A
sum AFS IMimdfetn  IMeimpcnt IMimpcntr IMimunplv IMimsmrgt  IMimscrlv ///
IMimtcjob IMimbleco IMimbgeco  IMimueclt IMimwbcnt IMimwbcrm IMimdetbs IMimdetmr IMstimrdt ,format
tab1 ifriends icolleagues1 male
sum age,format
tab iedulvl
tab iincome4
sum lrself2,format
log off partlog
tab icolleagues1,m
tab ifriend,m

*****************************************
******Table 2/Table B1/Table B3**********
*****************************************
*MODEL 0-Table 2
eststo clear
xtmixed AFS ||id:, mle
eststo M0
xtmixed, var

*Model 1a-Table 2a/Table B1a
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy ||id: , mle
eststo M1cRE
xtmixed, var
*FE/Hausman-Table B3a
xtreg AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  , fe
eststo M1cFE
hausman M1cFE M1cRE, eq(1:1) 

*Model 1b-Table 2b/Table B1b
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  ||id: , mle
eststo M1fRE
xtmixed, var
*FE/Hausman-Table B3b
xtreg AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  , fe
eststo M1fFE
hausman M1fFE M1fRE, eq(1:1) 

*Model 1c-Table 2c/Table B1c
xtmixed AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  ||id: , mle
eststo M1NcRE
xtmixed, var
*FE/Hausman-Table B3c
xtreg AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy , fe
eststo M1NcFE
hausman M1NcFE M1NcRE, eq(1:1)

*Model1d-Table 2d/Table B1d
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  ||id: , mle
eststo M1NfRE
xtmixed, var
*FE/Hausman-Table B3d
xtreg AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  , fe
eststo M1NfFE
hausman M1NfFE M1NfRE, eq(1:1)

log on partlog
*Table 2/TableB1
esttab M0 M1cRE M1fRE M1NcRE M1NfRE,label
*Table B3
esttab M1cFE M1fFE M1NcFE M1NfFE, label
log off partlog

*****************************************
******Table 3/Table B2/Table B4**********
*****************************************

*Model 2a-Table 3a/Table B2a
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues1#c.Mapmulti ||id:, mle
eststo M2cRE
xtmixed, var
*FE/Hausman-Table B4a
xtreg   AFS  i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues1#c.Mapmulti, fe
eststo M2cFE
hausman M2cFE M2cRE, eq(1:1)

*Model 2b-Table 3b/Table B2b
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  ifriends#c.Mapmulti ||id:, mle
eststo M2fRE
xtmixed,var
*FE/Hausman-Table B4b
xtreg AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  ifriends#c.Mapmulti, fe
eststo M2fFE
hausman M2fFE M2fRE, eq(1:1)

*Model 2c-Table 3c/Table B2c
xtmixed AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  icolleagues1#c.MapmultiN ||id:, mle
eststo M2NcRE
xtmixed,var
*FE/Hausman-Table B4c
xtreg AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  icolleagues1#c.MapmultiN, fe
eststo M2NcFE
hausman M2NcFE M2NcRE, eq(1:1)

*Model 2d-Table 3d/Table B2d
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy ifriends#c.MapmultiN ||id:, mle
eststo M2NfRE
xtmixed,var
*FE/Hausman-Table B4d
xtreg AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy ifriends#c.MapmultiN, fe
eststo M2NfFE
hausman M2NfFE M2NfRE, eq(1:1)

log on partlog
*Table 3/TableB2
esttab M2cRE M2fRE M2NcRE M2NfRE, label
*Table B4
esttab M2cFE M2fFE M2NcFE M2NfFE, label
log off partlog

**************
***Table B5***
**************
*B5a
xtmixed AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiL Cgdpcap Cforeign Cunemploy icolleagues1#c.MapmultiL ||id:, mle
eststo M2cREL
*B5b
xtmixed AFS i.ifriends male age i.iincome4 i.iedulvl lrself2 MapmultiL Cgdpcap Cforeign Cunemploy  ifriends#c.MapmultiL ||id:, mle
eststo M2fREL
*B5c
xtmixed AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiNL Cgdpcap Cforeign Cunemploy icolleagues1#c.MapmultiNL ||id:, mle
eststo M2NcREL
*B5c
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiNL Cgdpcap Cforeign Cunemploy ifriends#c.MapmultiNL ||id:, mle
eststo M2NfREL

log on partlog
***Table B5***
esttab M2cREL M2fREL M2NcREL M2NfREL,label
log off partlog
**************
***Table B6***
**************
*Model 2c
xtmixed AFS i.icolleagues1  male age i.iincome4 i.iedulvl lrself2 MapmultiR Cgdpcap Cforeign Cunemploy icolleagues1#c.MapmultiR ||id:, mle
eststo M2RcRE
*Model 2d
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 MapmultiR Cgdpcap Cforeign Cunemploy ifriends#c.MapmultiR ||id:, mle
eststo M2RfRE
log on partlog
***Table B6***
esttab M2RcRE M2RfRE,label
log off partlog

************************
*********FIGURES********
************************
*Figure 1 (Model 2a)
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy icolleagues1#c.Mapmulti ||id:, mle
estat sum
margins, dydx(icolleagues1) at(Mapmul=(0 (0.5) 8))

marginsplot, /// 
recastci(rline) /// 
ci1opts(lpattern(vshortdash) color(black)) ///
ci2opts(lpattern(vshortdash) color(black)) /// 
recast(line) /// 
plot1opts(lpattern(solid) color(black)) ///
plot2opts(lpattern(longdash) color(black)) ///
legend(on ///
order (3 4 1) ///
rows(3) /// 
label(3 "Difference between few colleagues and none") ///
label(4 "Difference between several colleagues and none") ///
label(1 "95% confidence interval") ///
size(small)) /// 
xtitle(Total political party focus (%)) /// 
ytitle("Predicted difference in AFS") /// 
yscale(range(-1.2 0)) ///
ylabel(-1.2(.2)0) ///
xlabel(0 (2) 8) ///
title("") /// 
name(colleagues,replace) ///
scheme(s1mono) // 
graph export "./results/Figure1.pdf", replace

*Figure B1 (Model2b)
xtmixed AFS i.ifriends  male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy ifriends#c.Mapmulti ||id:, mle
estat sum
margins, dydx(ifriends) at(Mapmul=(0 (0.5) 8))

marginsplot, /// 
recastci(rline) /// 
ci1opts(lpattern(vshortdash) color(black)) ///
ci2opts(lpattern(vshortdash) color(black)) /// 
recast(line) ///
plot1opts(lpattern(solid) color(black)) ///
plot2opts(lpattern(longdash) color(black)) ///
legend(on ///
order (3 4 1) ///
rows(3) /// 
label(3 "Difference between few friends and none") ///
label(4 "Difference between several friends and none") ///
label(1 "95% confidence interval") ///
size(small)) /// 
xtitle(Total political party focus (%)) /// 
ytitle("Predicted difference in AFS") /// 
yscale(range(-1.4 0)) ///
ylabel(-1.4(.2)0) ///
xlabel(0 (2) 8) ///
title("") ///
name(Friends,replace) ///
scheme(s1mono) // 

graph export "./results/FigureB1.pdf",replace
	
*****************Additional statistics

preserve
use "./gendata/maindata.dta",clear
log on partlog 
*Factor analysis (full sample)
factor IMimdetbs IMimdetmr IMimtcjob IMimbleco IMimbgeco IMimueclt IMimwbcnt IMimwbcrm IMimsmrgt IMimunplv IMimscrlv IMstimrdt IMimdfetn IMeimpcnt IMimpcntr,pcf
*corr between friendship and colleagues
tab icolleagues1 ifriend,taub
restore

*Chronbach's alpha
alpha IMimdetbs IMimdetmr IMimtcjob IMimbleco IMimbgeco IMimueclt IMimwbcnt IMimwbcrm IMimsmrgt IMimunplv IMimscrlv IMstimrdt IMimdfetn IMeimpcnt IMimpcntr,i c
*whitin country
levelsof cntry, local(cname)
foreach coname in `cname' {
	qui: alpha IMimdetbs IMimdetmr IMimtcjob IMimbleco IMimbgeco IMimueclt IMimwbcnt IMimwbcrm IMimsmrgt IMimunplv IMimscrlv IMstimrdt IMimdfetn IMeimpcnt IMimpcnt	if cntry=="`coname'", i c 
	dis "`coname': " `r(alpha)' " (alpha)"
}


********************************************************
********************Robustness analyses*****************
********************************************************
*Different Measures of colleagues - Note a in Appendix A
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy icolleagues1#c.Mapmulti ||id:, mle //original
xtmixed AFS ib2.icolleagues2 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues2#c.Mapmulti ||id:, mle //4 cat
xtmixed AFS i.icolleagues3 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues3#c.Mapmulti ||id:, mle  //Not working dropped

********Note c in Appendix A
*Dropping missing on education 
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulv2 lrself2 Mapmulti  Cgdpcap Cforeign Cunemploy  icolleagues1#c.Mapmulti  ||id:, mle //colleagues a
xtmixed  AFS i.ifriends    male age i.iincome4 i.iedulv2 lrself2 Mapmulti  Cgdpcap Cforeign Cunemploy  ifriends#c.Mapmulti      ||id:, mle //friends b 
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulv2 lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  icolleagues1#c.MapmultiN ||id:, mle //colleagues c 
xtmixed  AFS i.ifriends    male age i.iincome4 i.iedulv2 lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  ifriends#c.MapmultiN     ||id:, mle //friends d

*Dropping missing on education
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues1#c.Mapmulti if iincome4<5 ||id:, mle
xtmixed  AFS i.ifriends    male age i.iincome4 i.iedulvl lrself2 Mapmulti Cgdpcap Cforeign Cunemploy  ifriends#c.Mapmulti     if iincome4<5 ||id:, mle
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  icolleagues1#c.MapmultiN if iincome4<5 ||id:, mle
xtmixed  AFS i.ifriends    male age i.iincome4 i.iedulvl lrself2 MapmultiN Cgdpcap Cforeign Cunemploy  ifriends#c.MapmultiN    if iincome4<5 ||id:, mle

*Dropping missing on political ideology 
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself1 Mapmulti Cgdpcap Cforeign Cunemploy  icolleagues1#c.Mapmulti, mle //a
xtmixed  AFS i.ifriends  male age i.iincome4 i.iedulvl lrself1 Mapmulti Cgdpcap Cforeign Cunemploy  ifriends#c.Mapmulti ||id:, mle //b
xtmixed AFS i.icolleagues1 male age i.iincome4 i.iedulvl lrself1 MapmultiN Cgdpcap Cforeign Cunemploy icolleagues1#c.MapmultiN, mle //c
xtmixed  AFS i.ifriends  male age i.iincome4 i.iedulvl lrself1 MapmultiN Cgdpcap Cforeign Cunemploy  ifriends#c.MapmultiN ||id:, mle //d


*Don't know analyses/Table 4/Table B7
use "./gendata/maindata.dta",clear
xtnbreg totPOLICY Mapmulti otherMIS i.icolleagues1 male age i.iincome4 i.iedulvl lrself2 i.ifriends Cgdpcap Cforeign Cunemploy, re nolog

log close partlog
exit
